* Multiproduct Firms, Horizontal Mergers, and International Trade
* Jackie M.L. Chan, Michael Irlacher, and Michael Koch

* This do file runs additional regressions for export market outcomes.

* Setup
use "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\regression_data_domestic.dta", clear

merge 1:1 cvrnrA aarA using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_exports_all_cvrnr_dist_v2.dta", keep(match master) gen(_mergedist)

areg theil_fy p4 p3 p2 p1 test m1 m2 m3 m4 D_AA_1-D_AA_22 if minemployee_cvrnr>=10 & ((acquirer==1 & sdaar==0 & meanaar!=.) | acquirer==0) & _mergedist==3, absorb(cvrnrA) cluster(ind)
gen samp = e(sample)
keep if samp==1
keep cvrnrA aarA
tempfile tempsamp
save `tempsamp.dta', replace

***

*Main data

* From Do File #P9
use "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_did_imputation_prepare_jc_multi_d_si4_1865.dta", clear

* Match with VARS sample
merge 1:1 cvrnrA aarA using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_did_imputation_vars_prepare_jc_multi_d_si4_1865_dro.dta", keep(match master) keepusing()
egen max_merge = max(_merge) if aar!=., by(cvrnrA)
egen maxmax_merge = max(max_merge), by(cvrnrA)
drop if acquirer==1 & maxmax_merge!=3
drop _merge max_merge

* Merge with firm-level data
rename cvrnrA cvrnr
rename aar aar_ori
rename aarA aar
merge 1:1 cvrnr aar using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_firm_subset1865.dta", keep(match master) nogen
rename aar aarA
rename aar_ori aar
rename cvrnr cvrnrA

* From Do File #P11
merge 1:1 cvrnrA aarA using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_exports_all_cvrnr_dist_v2.dta", keep(match master) gen(_mergedist)

* From Do File #P5
merge 1:1 cvrnrA aarA using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_exports_nprodpermark.dta", keep(match master) nogen

rename lnnprodpermark lnanprodpermark
rename lnmaxnprodpermark lnamaxnprodpermark

gen nproddivnmark = nprod/nmark
gen lnnproddivnmark = ln(nproddivnmark)

* Drop non-acquirers not in manufacturing nor raw material extraction industries
gen BC = (CVR_BRANCHE_DB07>=100000 & CVR_BRANCHE_DB07<340000)
drop if acquirer== 0 & BC==0

* Drop if merger event occurs before 1999 (not enough years for pre-merger period)
drop if aar<1999 & acquirer==1

* Drop if acquirer has no merger event in remaining sample
egen meanaar = mean(aar), by(cvrnrA)
drop if meanaar==. & acquirer==1

* Drop if acquirer has multiple merger events
egen sdaar = sd(aar), by(cvrnrA)
drop if sdaar>0 & sdaar!=.
egen minnprod = min(nprod_dro) if nprod_dro>0 & nprod_dro!=., by(cvrnrA)

* Additional calculations
replace d_yr = aarA - meanaar

gen p4 = (d_yr>3 & acquirer==1)
gen m4 = (d_yr<-3 & acquirer==1)

merge 1:1 cvrnrA aarA using `tempsamp.dta', keep(match) nogen

* Calculate minimum number of employees in sample
egen minemployee_cvrnr = min(employee_cvrnr) if nprod>0 & nprod!=., by(cvrnrA)

* From Do File #P5
rename aar aar_ori
rename meanaar aar
merge 1:1 cvrnrA aar aarA using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_exports_nprodpermark_expb4.dta", keep(match master)
egen max_merge = max(_merge), by(cvrnrA)
drop _merge

replace lnnprodpermark = lnanprodpermark if acquirer==0
replace lnmaxnprodpermark = lnamaxnprodpermark if acquirer==0

rename aar meanaar
rename aar_ori aar

* Define spells for imputation estimator
gsort cvrnrA -aarA
by cvrnrA: carryforward aar, gen(aar2)
sort cvrnrA aarA
by cvrnrA: carryforward aar2, gen(aar3)
replace aar3 = aar3+1

* Figure A.15(a),(b)
foreach x in lnnmark lnnprod {
did_imputation `x' group_cvrnr aarA aar3 if minemployee_cvrnr>=10 & _mergedist==3, fe(group_cvrnr aarA) hor(0/2) pretrend(4) tol(0.0001) maxit(250) autosample cluster(ind)
outreg2 using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\output\table_figureA15.txt", bdec(4) sdec(4) nocons nonotes
}

foreach x in lnnprodpermark {	
did_imputation `x' group_cvrnr aarA aar3 if minemployee_cvrnr>=10 & ((acquirer==1 & max_merge==3) | acquirer==0) & _mergedist==3, fe(group_cvrnr aarA) hor(0/2) pretrend(4) tol(0.0001) maxit(250) autosample cluster(ind)
outreg2 using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\output\table_figureA15.txt", bdec(4) sdec(4) nocons nonotes 
}

foreach x in p90_ldistw {
did_imputation `x' group_cvrnr aarA aar3 if minemployee_cvrnr>=10 & _mergedist==3, fe(group_cvrnr aarA) hor(0/2) pretrend(4) tol(0.0001) maxit(250) autosample cluster(ind)
outreg2 using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\output\table_figureA15.txt", bdec(4) sdec(4) nocons nonotes 
}